package cn.wen.core.titlebar;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

/**
 * 状态栏的Util类
 */

public class TitleBarUtil {

    public static final String TRANSPARENT = "#00FFFFFF";

    /**
     * 设置状态栏的颜色
     *
     * @param activity
     * @param color
     */
    public static void setStatusBarColor(Activity activity, int color) {
        //4.4以下
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            return;
        }
        // 设置为透明的状态栏
        // 5.0以上
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            activity.getWindow().setStatusBarColor(color);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //4.4- 5.0之间的
            // 创建一个View
            View view = new View(activity);
            view.setBackgroundColor(color);
            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity)));
            // 获取到decorView
            ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
            decorView.addView(view);
        }
    }

    /**
     * 设置状态栏为透明
     */
    public static void setStatusBarColor(Activity activity) {
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        setStatusBarColor(activity, Color.parseColor(TRANSPARENT));
    }

    /**
     * 设置TitleBar的高度
     */
    public static void setTitlePadding(Activity activity, View titleView) {
        if (titleView == null) {
            return;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            titleView.setPadding(0, getStatusBarHeight(activity), 0, 0);
        } else {
            titleView.setPadding(0, 0, 0, 0);
        }
    }

    /**
     * 获取状态栏的高度
     */
    private static int getStatusBarHeight(Activity activity) {
        Resources resources = activity.getResources();
        int identifier = resources.getIdentifier("status_bar_height", "dimen", "android");
        return resources.getDimensionPixelOffset(identifier);
    }


}
